<template>
	<view>
		<view class="swiperWp">
			<u-swiper height='400' imgMode='aspectFill' :list="fileListImg" keyName="filePath" :autoplay="true"
				circular>
			</u-swiper>
		</view>
		<view class="containWp">
			<view class="itemWp1">
				<view class="title">
					上传轮播图
				</view>
				<u-upload :fileList="fileListImg" @afterRead="afterRead" @delete="deletePic" name="Img" :multiple='true'
					:maxCount="5">
					<view class="uploadWp">
						<u-icon name="plus" color="#6863fe" size="90"></u-icon>
					</view>
				</u-upload>
			</view>
		</view>
		<view class="buttonWp">
			<u-button type="primary" size="block" text="提交"></u-button>
		</view>
	</view>
</template>

<script>
	import baseConfig from '@/config/baseUrl.js'
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				cardId: '',
				companyId: '',
				fileListImg: [],
			}
		},
		onLoad(opt) {
			if (opt.cardId) {
				this.cardId = opt.cardId
			}
			if (opt.companyId) {
				this.companyId = opt.companyId
			}

		},
		methods: {
			// 新增图片
			async afterRead(event) {
				console.log('新增图片', event);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileListImg.length
				lists.map((item) => {
					this.fileListImg.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileListImg[fileListLen]
					this.fileListImg.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: event.name,
						url: result,
						fileName: event.file[0].type + event.file[0].size + '',
						filePath: result
					}))
					fileListLen++
				}
				console.log(this.fileListImg, 'img')
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.success) {
								resolve(data.message)
							}
						}
					});
				})
			}, // 删除图片
			deletePic(event) {
				this.fileListImg.splice(event.index, 1)
			},
		},
		computed: {
			...mapGetters(['userInfo']),
		}
	}
</script>

<style lang="scss">
	.containWp {
		.title {
			text-align: center;
			margin: 30upx;
		}

		.itemWp1 {
			margin: 0 20upx;
			padding-bottom: 20upx;
			// display: flex;
			// align-items: center;

			.uploadWp {
				width: 90upx;
				border: 1px solid #ccc;
			}
		}

		.itemWp2 {
			margin: 0 20upx;
			padding-bottom: 20upx;
			display: flex;
			align-items: center;

			.uploadWp {
				width: 90upx;
				border: 1px solid #ccc;
			}
		}

	}

	//轮播图
	.swiperWp {
		margin-top: 50upx;


	}

	.buttonWp {
		margin: 30rpx;
		display: flex;
		justify-content: center;
	}
</style>
